# Vue plugin

import { SourceCode } from 'rspress/theme';

<SourceCode href="https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin-vue" />

The Vue plugin provides support for Vue 3 SFC (Single File Components). The plugin internally integrates [vue-loader](https://vue-loader.vuejs.org/) v17.

:::tip
For Vue 3 JSX / TSX syntax, please use the [Vue JSX plugin](https://github.com/rspack-contrib/rsbuild-plugin-vue-jsx).
:::

## Quick start

### Install plugin

You can install the plugin using the following command:

import { PackageManagerTabs } from '@theme';

<PackageManagerTabs command="add @rsbuild/plugin-vue -D" />

### Register plugin

You can register the plugin in the `rsbuild.config.ts` file:

```ts title="rsbuild.config.ts"
import { pluginVue } from '@rsbuild/plugin-vue';

export default {
  plugins: [pluginVue()],
};
```

After registering the plugin, you can import `*.vue` SFC files in your code.

## Options

To customize the compilation behavior of Vue, use the following options.

### vueLoaderOptions

Options passed to `vue-loader`, please refer to the [vue-loader documentation](https://vue-loader.vuejs.org/) for detailed usage.

- **Type:** `VueLoaderOptions`
- **Default:**

```js
const defaultOptions = {
  compilerOptions: {
    preserveWhitespace: false,
  },
  experimentalInlineMatchResource: true,
};
```

- **Example:**

```ts
pluginVue({
  vueLoaderOptions: {
    hotReload: false,
  },
});
```

### splitChunks

When [chunkSplit.strategy](/config/performance/chunk-split) set to `split-by-experience`, Rsbuild will automatically split `vue` and `router` related packages into separate chunks by default:

- `lib-vue.js`: includes vue, vue-loader, and vue's sub-dependencies (@vue/shared, @vue/reactivity, @vue/runtime-dom, @vue/runtime-core).
- `lib-router.js`: includes vue-router.

This option is used to control this behavior and determine whether the `vue` and `router` related packages need to be split into separate chunks.

- **Type:**

```ts
type SplitVueChunkOptions = {
  vue?: boolean;
  router?: boolean;
};
```

- **Default:**

```ts
const defaultOptions = {
  vue: true,
  router: true,
};
```

- **Example:**

```ts
pluginVue({
  splitChunks: {
    vue: false,
    router: false,
  },
});
```

## FAQ

### /deep/ selector causes compilation error

`/deep/` is a deprecated usage as of Vue v2.7. Since it is not a valid CSS syntax, CSS compilation tools like Lightning CSS will fail to compile it.

You can use `:deep()` instead. See [Vue - Deep Selectors](https://vuejs.org/api/sfc-css-features.html#deep-selectors) for more details.

```html
<style scoped>
  .a :deep(.b) {
    /* ... */
  }
</style>
```

> You can also refer to [Vue - RFC 0023](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0023-scoped-styles-changes.md) for more details.
